package pl.plesken.swt;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;

import org.apache.xmlbeans.XmlException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import pl.plesken.graphic.CreateCoverImage;
import pl.plesken.graphic.GraphicTools;
import pl.plesken.html.HTMLCreator;
import pl.plesken.languages.SetLanguage;
import pl.plesken.net.JWGet;
import pl.plesken.tools.FileCode;
import pl.plesken.xml.ConfigParser;
import pl.plesken.xml.EditXMLFile;

import com.swtdesigner.SWTResourceManager;

public class Editor {

	private StyledText boxStyledText;
	private Text urlText;
	private Combo selectImageCombo;
	private Table editorTable;
	private Canvas imageCanvas;
	String fileInputXMLName;
	String dirInputXMLName;
	StringBuffer spinner = new StringBuffer();
	StringBuffer genres = new StringBuffer();
	StringBuffer actors = new StringBuffer();
	StringBuffer subtitles = new StringBuffer();
	StringBuffer imageFileName = new StringBuffer();
	StringBuffer imageFileType = new StringBuffer();
	StringBuffer FanartURL = new StringBuffer();
	StringBuffer PosterURL = new StringBuffer();
	private StyledText genresStyledText;
	private StyledText actorsStyledText;
	private StyledText plotStyledText;
	private Text outlineText;
	private Text videoOutputText;
	private Text videoSourceText;
	private Text resolutionText;
	private Text fpsText;
	private Text audioCodecText;
	private Text videoCodecText;
	private Text containerText;
	private Text languageText;
	private Text runtimeText;
	private Text countryText;
	private Text directorText;
	private Text releaseText;
	private Text yearText;
	private Text originalTitleText;
	private Text titleText;
	private Image imageFile;
	protected Shell shell;
	LinkedList<String> rowFiles = new LinkedList<String>();


	public String getFileInputXMLName() {
		return fileInputXMLName;
	}

	public void setFileInputXMLName(String fileInputXMLName) {
		this.fileInputXMLName = fileInputXMLName;
	}

	public String getDirInputXMLName() {
		return dirInputXMLName;
	}

	public void setDirInputXMLName(String dirInputXMLName) {
		this.dirInputXMLName = dirInputXMLName;
	}
	
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Editor window = new Editor();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		FileCode code = new FileCode();
		EditXMLFile getXMLFile = new EditXMLFile();
		
		getXMLFile.getParameters(dirInputXMLName + File.separator + fileInputXMLName  + ".xml"); //$NON-NLS-1$
		getXMLFile.files(dirInputXMLName + File.separator + fileInputXMLName  + ".xml"); //$NON-NLS-1$
		
		try {
			FanartURL.setLength(0);
			FanartURL.append(code.decode(getXMLFile.getFanartURL())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			PosterURL.setLength(0);
			PosterURL.append(code.decode(getXMLFile.getPosterURL())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		} catch(NullPointerException e) {
			e.printStackTrace();
		} catch(UnsupportedEncodingException e) {
			e.printStackTrace();
		}
			
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(Editor.class, "/icons/gui.png")); //$NON-NLS-1$
		shell.setLayout(new GridLayout());
		shell.setSize(744, 669);
		shell.setText(fileInputXMLName);

		final TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final TabItem detailsTableItem = new TabItem(tabFolder, SWT.NONE);
		detailsTableItem.setText(SetLanguage.getString("Editor.detailsTableItem")); //$NON-NLS-1$

		final Composite mainComposite = new Composite(tabFolder, SWT.NONE);
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 1;
		mainComposite.setLayout(gridLayout_1);
		detailsTableItem.setControl(mainComposite);

		final Group detailsGroup = new Group(mainComposite, SWT.NONE);
		final GridData gd_detailsGroup = new GridData(SWT.FILL, SWT.CENTER, true, false);
		detailsGroup.setLayoutData(gd_detailsGroup);
		final GridLayout gridLayout_2 = new GridLayout();
		gridLayout_2.numColumns = 5;
		detailsGroup.setLayout(gridLayout_2);

		final Label titleLabel = new Label(detailsGroup, SWT.RIGHT);
		titleLabel.setText(SetLanguage.getString("Editor.titleLabel")); //$NON-NLS-1$

		titleText = new Text(detailsGroup, SWT.BORDER);
		titleText.setText(getXMLFile.getTitle());
		titleText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final Label empty = new Label(detailsGroup, SWT.NONE);
		empty.setText("   "); //$NON-NLS-1$

		final Label sortTitleLabel = new Label(detailsGroup, SWT.RIGHT);
		sortTitleLabel.setText(SetLanguage.getString("Editor.originalTitleLabel")); //$NON-NLS-1$

		originalTitleText = new Text(detailsGroup, SWT.BORDER);
		originalTitleText.setText(getXMLFile.getOriginalTitle());
		final GridData gd_originalTitleText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		originalTitleText.setLayoutData(gd_originalTitleText);

		final Label yearLabel = new Label(detailsGroup, SWT.RIGHT);
		yearLabel.setText(SetLanguage.getString("Editor.yearLabel")); //$NON-NLS-1$

		yearText = new Text(detailsGroup, SWT.BORDER);
		yearText.setText(getXMLFile.getYear());
		yearText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		new Label(detailsGroup, SWT.NONE);

		final Label releaseLabel = new Label(detailsGroup, SWT.RIGHT);
		releaseLabel.setText(SetLanguage.getString("Editor.releaseYearLabel")); //$NON-NLS-1$

		releaseText = new Text(detailsGroup, SWT.BORDER);
		releaseText.setText(getXMLFile.getReleaseDate());
		releaseText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final Label directorLabel = new Label(detailsGroup, SWT.RIGHT);
		directorLabel.setText(SetLanguage.getString("Editor.directorLabel")); //$NON-NLS-1$

		directorText = new Text(detailsGroup, SWT.BORDER);
		directorText.setText(getXMLFile.getDirector());
		directorText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		new Label(detailsGroup, SWT.NONE);

		final Label countryLabel = new Label(detailsGroup, SWT.RIGHT);
		countryLabel.setText(SetLanguage.getString("Editor.countryLabel")); //$NON-NLS-1$

		countryText = new Text(detailsGroup, SWT.BORDER);
		countryText.setText(getXMLFile.getCountry());
		countryText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final Label runtimeLabel = new Label(detailsGroup, SWT.RIGHT);
		runtimeLabel.setText(SetLanguage.getString("Editor.runtimeLabel")); //$NON-NLS-1$

		runtimeText = new Text(detailsGroup, SWT.BORDER);
		runtimeText.setText(getXMLFile.getRuntime());
		runtimeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		new Label(detailsGroup, SWT.NONE);

		final Label ratingLabel = new Label(detailsGroup, SWT.RIGHT);
		ratingLabel.setText(SetLanguage.getString("Editor.ratingLabel")); //$NON-NLS-1$

		final Spinner ratingSpinner = new Spinner(detailsGroup, SWT.BORDER);
		ratingSpinner.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				spinner.setLength(0);
				spinner.append(ratingSpinner.getText());
			}
		});
		ratingSpinner.setSelection(Integer.valueOf(getXMLFile.getRating()));
		spinner.setLength(0);
		spinner.append(ratingSpinner.getText());
		ratingSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		final Label languageLabel = new Label(detailsGroup, SWT.RIGHT);
		languageLabel.setText(SetLanguage.getString("Editor.languageLabel")); //$NON-NLS-1$

		languageText = new Text(detailsGroup, SWT.BORDER);
		languageText.setText(getXMLFile.getLanguage());
		languageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		new Label(detailsGroup, SWT.NONE);
		new Label(detailsGroup, SWT.NONE);

		final Button subsButton = new Button(detailsGroup, SWT.CHECK);
		subsButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (subsButton.getSelection() == true) {
					subtitles.setLength(0);
					subtitles.append("YES"); //$NON-NLS-1$
				}
				if (subsButton.getSelection() == false) {
					subtitles.setLength(0);
					subtitles.append("NO"); //$NON-NLS-1$
				}				
			}
		});
		if (getXMLFile.getSubtitles().equals("YES")) { //$NON-NLS-1$
			subsButton.setSelection(true);
			subtitles.setLength(0);
			subtitles.append("YES"); //$NON-NLS-1$
		} else {
			subsButton.setSelection(false);
			subtitles.setLength(0);
			subtitles.append("NO"); //$NON-NLS-1$
		}
		subsButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		subsButton.setText(SetLanguage.getString("Editor.subtitlesButton")); //$NON-NLS-1$

		final Label containerLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_containerLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		containerLabel.setLayoutData(gd_containerLabel);
		containerLabel.setText(SetLanguage.getString("Editor.containerLabel")); //$NON-NLS-1$

		containerText = new Text(detailsGroup, SWT.BORDER);
		containerText.setText(getXMLFile.getContainer());
		final GridData gd_containerText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		containerText.setLayoutData(gd_containerText);
		new Label(detailsGroup, SWT.NONE);
		new Label(detailsGroup, SWT.NONE);
		new Label(detailsGroup, SWT.NONE);

		final Label videoCodecLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_videoCodecLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		videoCodecLabel.setLayoutData(gd_videoCodecLabel);
		videoCodecLabel.setText(SetLanguage.getString("Editor.videoCodecLabel")); //$NON-NLS-1$

		videoCodecText = new Text(detailsGroup, SWT.BORDER);
		videoCodecText.setText(getXMLFile.getVideoCodec());
		final GridData gd_videoCodecText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		videoCodecText.setLayoutData(gd_videoCodecText);
		new Label(detailsGroup, SWT.NONE);

		final Label audioCodecLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_audioCodecLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		audioCodecLabel.setLayoutData(gd_audioCodecLabel);
		audioCodecLabel.setText(SetLanguage.getString("Editor.audioCodecLabel")); //$NON-NLS-1$

		audioCodecText = new Text(detailsGroup, SWT.BORDER);
		audioCodecText.setText(getXMLFile.getAudioCodec());
		final GridData gd_audioCodecText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		audioCodecText.setLayoutData(gd_audioCodecText);

		final Label fpsLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_fpsLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		fpsLabel.setLayoutData(gd_fpsLabel);
		fpsLabel.setText(SetLanguage.getString("Editor.fpsLabel")); //$NON-NLS-1$

		fpsText = new Text(detailsGroup, SWT.BORDER);
		fpsText.setText(getXMLFile.getFps());
		final GridData gd_fpsText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		fpsText.setLayoutData(gd_fpsText);
		new Label(detailsGroup, SWT.NONE);

		final Label resolutionLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_resolutionLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		resolutionLabel.setLayoutData(gd_resolutionLabel);
		resolutionLabel.setText(SetLanguage.getString("Editor.resolutionLabel")); //$NON-NLS-1$

		resolutionText = new Text(detailsGroup, SWT.BORDER);
		resolutionText.setText(getXMLFile.getResolution());
		final GridData gd_resolutionText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		resolutionText.setLayoutData(gd_resolutionText);

		final Label videoSourceLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_videoSourceLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		videoSourceLabel.setLayoutData(gd_videoSourceLabel);
		videoSourceLabel.setText(SetLanguage.getString("Editor.videoSourceLabel")); //$NON-NLS-1$

		videoSourceText = new Text(detailsGroup, SWT.BORDER);
		videoSourceText.setText(getXMLFile.getVideoSource());
		final GridData gd_videoSourceText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		videoSourceText.setLayoutData(gd_videoSourceText);
		new Label(detailsGroup, SWT.NONE);

		final Label videoOutputLabel = new Label(detailsGroup, SWT.NONE);
		final GridData gd_videoOutputLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		videoOutputLabel.setLayoutData(gd_videoOutputLabel);
		videoOutputLabel.setText(SetLanguage.getString("Editor.videoSystemLabel")); //$NON-NLS-1$

		videoOutputText = new Text(detailsGroup, SWT.BORDER);
		videoOutputText.setText(getXMLFile.getVideoOutput());
		final GridData gd_videoOutputText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		videoOutputText.setLayoutData(gd_videoOutputText);

		final Group plotGroup = new Group(mainComposite, SWT.NONE);
		final GridData gd_plotGroup = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_plotGroup.heightHint = 100;
		plotGroup.setLayoutData(gd_plotGroup);
		final GridLayout gridLayout_3 = new GridLayout();
		gridLayout_3.numColumns = 2;
		plotGroup.setLayout(gridLayout_3);

		final Label outlineLabel = new Label(plotGroup, SWT.NONE);
		final GridData gd_outlineLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		outlineLabel.setLayoutData(gd_outlineLabel);
		outlineLabel.setText(SetLanguage.getString("Editor.outlineLabel")); //$NON-NLS-1$

		outlineText = new Text(plotGroup, SWT.BORDER);
		outlineText.setText(getXMLFile.getOutline());
		final GridData gd_outlineText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		outlineText.setLayoutData(gd_outlineText);

		final Label plotLabel = new Label(plotGroup, SWT.NONE);
		final GridData gd_plotLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		plotLabel.setLayoutData(gd_plotLabel);
		plotLabel.setText(SetLanguage.getString("Editor.generalPlotLabel")); //$NON-NLS-1$

		plotStyledText = new StyledText(plotGroup, SWT.V_SCROLL | SWT.BORDER);
		plotStyledText.setText(getXMLFile.getPlot());
		final GridData gd_plotStyledText = new GridData(SWT.FILL, SWT.FILL, true, true);
		plotStyledText.setLayoutData(gd_plotStyledText);

		final Composite infoComposite = new Composite(mainComposite, SWT.NONE);
		final GridLayout gridLayout_4 = new GridLayout();
		gridLayout_4.numColumns = 2;
		infoComposite.setLayout(gridLayout_4);
		final GridData gd_infoComposite = new GridData(SWT.FILL, SWT.FILL, true, true);
		infoComposite.setLayoutData(gd_infoComposite);

		final Group actorsGroup = new Group(infoComposite, SWT.NONE);
		actorsGroup.setText(SetLanguage.getString("Editor.actorsGroup")); //$NON-NLS-1$
		final GridData gd_actorsGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
		actorsGroup.setLayoutData(gd_actorsGroup);
		actorsGroup.setLayout(new GridLayout());

		actorsStyledText = new StyledText(actorsGroup, SWT.V_SCROLL | SWT.BORDER);
		try {
			actors.setLength(0);
			String actor[] = getXMLFile.getActors().split(";"); //$NON-NLS-1$
			for (int i = 0; i < actor.length; i++) {
				if (i == 0) {
					actors.append((actor[i]));
				} else {
					actors.append("\n" + actor[i]); //$NON-NLS-1$
				}
			}
		} catch(NullPointerException e) {
			e.printStackTrace();
		}
		actorsStyledText.setText(actors.toString());	
		final GridData gd_actorsStyledText = new GridData(SWT.FILL, SWT.FILL, true, true);
		actorsStyledText.setLayoutData(gd_actorsStyledText);

		final Group genresGroup = new Group(infoComposite, SWT.NONE);
		genresGroup.setText(SetLanguage.getString("Editor.genresGroup")); //$NON-NLS-1$
		final GridData gd_genresGroup = new GridData(SWT.FILL, SWT.FILL, true, true);
		genresGroup.setLayoutData(gd_genresGroup);
		genresGroup.setLayout(new GridLayout());

		genresStyledText = new StyledText(genresGroup, SWT.V_SCROLL | SWT.BORDER);
		try {
			genres.setLength(0);
			String genre[] = getXMLFile.getGenres().split(";"); //$NON-NLS-1$
			for (int i = 0; i < genre.length; i++) {
				if (i == 0) {
					genres.append((genre[i]));
				} else {
					genres.append("\n" + genre[i]); //$NON-NLS-1$
				}
			}
		} catch(NullPointerException e) {
			e.printStackTrace();
		}
		genresStyledText.setText(genres.toString());
		final GridData gd_genresStyledText = new GridData(SWT.FILL, SWT.FILL, true, true);
		genresStyledText.setLayoutData(gd_genresStyledText);

		final TabItem filesTabItem = new TabItem(tabFolder, SWT.NONE);
		filesTabItem.setText(SetLanguage.getString("Editor.partsTabItem")); //$NON-NLS-1$

		final Composite filesComposite = new Composite(tabFolder, SWT.NONE);
		filesComposite.setLayout(new GridLayout());
		filesTabItem.setControl(filesComposite);

		final Group filesGroup = new Group(filesComposite, SWT.NONE);
		filesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		filesGroup.setLayout(new GridLayout());

		final Label label = new Label(filesGroup, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(0, 0, 255));
		label.setFont(SWTResourceManager.getFont("Sans", 18, SWT.BOLD | SWT.ITALIC)); //$NON-NLS-1$
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
		label.setText(getXMLFile.getTitle());

		editorTable = new Table(filesGroup, SWT.BORDER);
		editorTable.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(final MouseEvent e) {
				editPlot();
			}
		});
		editorTable.setLinesVisible(true);
		editorTable.setHeaderVisible(true);
		final GridData gd_editorTable = new GridData(SWT.FILL, SWT.FILL, true, true);
		editorTable.setLayoutData(gd_editorTable);

		final TableColumn partTableColumn = new TableColumn(editorTable, SWT.CENTER);
		partTableColumn.setWidth(60);
		partTableColumn.setText(SetLanguage.getString("Editor.partTableColumn")); //$NON-NLS-1$

		final TableColumn titleTableColumn = new TableColumn(editorTable, SWT.NONE);
		titleTableColumn.setWidth(200);
		titleTableColumn.setText(SetLanguage.getString("Editor.titleTableColumn")); //$NON-NLS-1$

		final TableColumn pathTableColumn = new TableColumn(editorTable, SWT.NONE);
		pathTableColumn.setText(SetLanguage.getString("Editor.pathTableColumn")); //$NON-NLS-1$

		final TableColumn subtitlesExchangeTableColumn = new TableColumn(editorTable, SWT.NONE);
		subtitlesExchangeTableColumn.setText("New column"); //$NON-NLS-1$

		final TableColumn plotTableColumn = new TableColumn(editorTable, SWT.NONE);
		plotTableColumn.setWidth(400);
		plotTableColumn.setText(SetLanguage.getString("Editor.plotTableColumn")); //$NON-NLS-1$

		final Group manageGroup = new Group(filesGroup, SWT.NONE);
		manageGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout_7 = new GridLayout();
		manageGroup.setLayout(gridLayout_7);

		final Button delButton = new Button(manageGroup, SWT.NONE);
		delButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				delPlot();
			}
		});
		final GridData gd_delButton = new GridData(SWT.RIGHT, SWT.CENTER, true, false);
		gd_delButton.widthHint = 30;
		delButton.setLayoutData(gd_delButton);
		delButton.setText("-"); //$NON-NLS-1$
		
		//Load data to editTable
		//if (!getXMLFile.getFiles().isEmpty()) {
			try {
				for (int s = 0; s < getXMLFile.getRowFiles().size(); s++) {
					String[] tab = getXMLFile.getRowFiles().get(s).split("<BREAK_TABLE>"); //$NON-NLS-1$
					
					TableItem editItem = new TableItem(editorTable, SWT.NULL);
					editItem.setText(0, tab[0].toString());
					editItem.setText(1, tab[1].toString());
					editItem.setText(2, tab[2].toString());
					editItem.setText(3, tab[3].toString());
					
					if (tab.length == 5) {
						editItem.setText(4, tab[4].toString());
					} else {
						editItem.setText(4, "EMPTY"); //$NON-NLS-1$
					}
				}
			} catch(NullPointerException e) {
				System.out.println(e);
			}
		//}

		final TabItem imagesTabItem = new TabItem(tabFolder, SWT.NONE);
		imagesTabItem.setText(SetLanguage.getString("Editor.picturesTabItem")); //$NON-NLS-1$

		final TabItem boxTabItem = new TabItem(tabFolder, SWT.NONE);
		boxTabItem.setText(SetLanguage.getString("Editor.box")); //$NON-NLS-1$

		final Composite boxesComposite = new Composite(tabFolder, SWT.NONE);
		boxesComposite.setLayout(new GridLayout());
		boxTabItem.setControl(boxesComposite);

		final Group boxesGroup = new Group(boxesComposite, SWT.NONE);
		boxesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		boxesGroup.setLayout(new GridLayout());

		boxStyledText = new StyledText(boxesGroup, SWT.V_SCROLL | SWT.BORDER);
		//Load sets
		try {
			boxStyledText.setText(loadSets(getXMLFile.getBox()));
		} catch(NullPointerException e) {
			e.printStackTrace();
		}
		boxStyledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Composite imagesComposite = new Composite(tabFolder, SWT.NONE);
		final GridLayout gridLayout_5 = new GridLayout();
		imagesComposite.setLayout(gridLayout_5);
		imagesTabItem.setControl(imagesComposite);

		final Group imagesGroup = new Group(imagesComposite, SWT.NONE);
		imagesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		imagesGroup.setLayout(new GridLayout());
		
		selectImageCombo = new Combo(imagesGroup, SWT.READ_ONLY);
		selectImageCombo.setItems(new String[] {"Fanart", "Poster"}); //$NON-NLS-1$ //$NON-NLS-2$
		selectImageCombo.setText("Poster"); //$NON-NLS-1$

		if (selectImageCombo.getText().equals("Poster")) { //$NON-NLS-1$
			imageFileName.append(dirInputXMLName + File.separator + fileInputXMLName + ".jpg"); //$NON-NLS-1$
			imageFileType.append("Poster"); //$NON-NLS-1$
		}
		
		selectImageCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
					comboListener();
				}
			});
		final GridData gd_selectImageCombo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		selectImageCombo.setLayoutData(gd_selectImageCombo);

		imageCanvas = new Canvas(imagesGroup, SWT.NONE);
		loadImage();
		final GridData gd_imageCanvas = new GridData(SWT.FILL, SWT.FILL, true, true);
		imageCanvas.setLayoutData(gd_imageCanvas);
		
		final Group downloadImageGroup = new Group(imagesComposite, SWT.NONE);
		downloadImageGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		final GridLayout gridLayout_6 = new GridLayout();
		gridLayout_6.numColumns = 3;
		downloadImageGroup.setLayout(gridLayout_6);

		final Label urlLabel = new Label(downloadImageGroup, SWT.NONE);
		urlLabel.setText("URL:"); //$NON-NLS-1$

		urlText = new Text(downloadImageGroup, SWT.BORDER);
		if (imageFileType.toString().equals("Poster")) { //$NON-NLS-1$
			urlText.setText(PosterURL.toString());
		}
		if (imageFileType.toString().equals("Fanart")) { //$NON-NLS-1$
			urlText.setText(FanartURL.toString());
		}
		final GridData gd_urlText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		urlText.setLayoutData(gd_urlText);

		final Button downloadButton = new Button(downloadImageGroup, SWT.NONE);
		downloadButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				downloadButton.setEnabled(false);
				
				/*MessageBox end = new MessageBox(shell, SWT.ICON_INFORMATION);
				end.setMessage("Kliknij \"OK\" aby zacząć pobierać.\nGdy obrazek zostanie ściągnięty, automatycznie\nzaładuje się do podglądu i zamknie się\nto okno.");  //$NON-NLS-1$
				end.setText("Pobieranie");   //$NON-NLS-1$
				end.open();*/
				
				downloadImage();
				
				downloadButton.setEnabled(true);
				
				imageCanvas.redraw();
			}
		});
		downloadButton.setText(SetLanguage.getString("Editor.downloadButton")); //$NON-NLS-1$

		final Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group.setLayout(gridLayout);

		final Button updateButton = new Button(group, SWT.NONE);
		updateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				updateXML();
			}
		});
		final GridData gd_updateButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_updateButton.widthHint = 100;
		updateButton.setLayoutData(gd_updateButton);
		updateButton.setText(SetLanguage.getString("Editor.setButton")); //$NON-NLS-1$

		final Button cancelButton = new Button(group, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_cancelButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_cancelButton.widthHint = 100;
		cancelButton.setLayoutData(gd_cancelButton);
		cancelButton.setText(SetLanguage.getString("Editor.cancelButton")); //$NON-NLS-1$
		//
	}
	
	private void comboListener() {
		imageFileName.setLength(0);
		imageFileType.setLength(0);
		
		if (selectImageCombo.getText().equals("Poster")) { //$NON-NLS-1$
			imageFileType.append("Poster"); //$NON-NLS-1$
			urlText.setText(PosterURL.toString());
			imageFileName.append(dirInputXMLName + File.separator + fileInputXMLName + ".jpg"); //$NON-NLS-1$
		}
		if (selectImageCombo.getText().equals("Fanart")) { //$NON-NLS-1$
			imageFileType.append("Fanart"); //$NON-NLS-1$
			urlText.setText(FanartURL.toString());
			imageFileName.append(dirInputXMLName + File.separator + fileInputXMLName + ".fanart.jpg"); //$NON-NLS-1$
		}		

		File fin = new File(imageFileName.toString());
		
		if (!fin.exists()) {
			if (imageFileType.toString().equals("Poster")) { //$NON-NLS-1$
				imageFileName.setLength(0);
				imageFileName.append("pictures/nopreview.jpg"); //$NON-NLS-1$
			}
			if (imageFileType.toString().equals("Fanart")) { //$NON-NLS-1$
				imageFileName.setLength(0);
				imageFileName.append("pictures/nopreview.fanart.jpg"); //$NON-NLS-1$
			}
		}
		
		imageCanvas.redraw();
		
	}
	
	private void loadImage() {
	    imageCanvas.addPaintListener(new PaintListener() {
	        public void paintControl(PaintEvent e) {
	        	imageFile = new Image(Display.getDefault(), imageFileName.toString());
	        	if (imageFileType.toString().equals("Poster")) { //$NON-NLS-1$
	        		int width = imageFile.getBounds().width;
	        		int height = imageFile.getBounds().height;
		        	e.gc.drawImage(imageFile, 0, 0, width, height, 0, 0, 288, 416);
	        	}
	        	if (imageFileType.toString().equals("Fanart")) { //$NON-NLS-1$
		        	int width = imageFile.getBounds().width;
		        	int height = imageFile.getBounds().height;
		        	e.gc.drawImage(imageFile, 0, 0, width, height, 0, 0, 704, 396);
	        	}
	        	imageFile.dispose();
	       	}
	    });
	}
	
	
	private void downloadImage() {
		try {
			JWGet link = new JWGet();
			GraphicTools convert = new GraphicTools(); 
			
			int fileLength = urlText.getText().length();
			String extension = urlText.getText().substring(fileLength - 3, fileLength);
					
			if (imageFileType.toString().equals("Poster")) { //$NON-NLS-1$
				PosterURL.setLength(0);
				PosterURL.append(urlText.getText());
				link.downloadFile(urlText.getText(), dirInputXMLName + File.separator + fileInputXMLName + "." + extension); //$NON-NLS-1$
				
				if (!extension.equalsIgnoreCase("jpg")) { //$NON-NLS-1$
					BufferedImage notJPG = convert.loadImage(dirInputXMLName + File.separator + fileInputXMLName + "." + extension); //$NON-NLS-1$
					convert.saveImageAsJpg(notJPG, dirInputXMLName + File.separator + fileInputXMLName + ".jpg"); //$NON-NLS-1$
				}
			}
			if (imageFileType.toString().equals("Fanart")) { //$NON-NLS-1$
				FanartURL.setLength(0);
				FanartURL.append(urlText.getText());
				link.downloadFile(urlText.getText(), dirInputXMLName + File.separator + fileInputXMLName + ".temp." + extension); //$NON-NLS-1$
				BufferedImage mustBeJPG = convert.loadImage(dirInputXMLName + File.separator + fileInputXMLName + ".temp." + extension); //$NON-NLS-1$
				mustBeJPG = convert.resizeImage(mustBeJPG, 1280, 720);
				convert.saveImageAsJpg(mustBeJPG, dirInputXMLName + File.separator + fileInputXMLName + ".fanart.jpg"); //$NON-NLS-1$
				
				File temp = new File(dirInputXMLName + File.separator + fileInputXMLName + ".temp." + extension); //$NON-NLS-1$
				temp.delete();
			}
			
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	private void delPlot() {
		editorTable.remove(editorTable.getSelectionIndex());
	}
	
	private void editPlot() {
		EditorPlot plot = new EditorPlot(shell);
		
		TableItem[] s = editorTable.getItems();
		
		plot.setPart(s[editorTable.getSelectionIndex()].getText(0));
		plot.setTitle(s[editorTable.getSelectionIndex()].getText(1));
		plot.setPlot(s[editorTable.getSelectionIndex()].getText(4));
		
		plot.open();
		
		s[editorTable.getSelectionIndex()].setText(1, plot.getTitle());
		s[editorTable.getSelectionIndex()].setText(4, plot.getPlot());
	}
	
	private String loadSets(String Set) throws NullPointerException {
		StringBuffer b = new StringBuffer();
		String name;
		
		if (!Set.equals("")) { //$NON-NLS-1$
			String[] val = Set.split("<BREAK>"); //$NON-NLS-1$
			b.setLength(0);
			for (int i = 0; i < val.length; i++) {
				String[] sep = val[i].split("<ORDER>"); //$NON-NLS-1$
				if (sep[1].equals("null")) { //$NON-NLS-1$
					name = sep[0];
				} else {
					name = sep[0] + "-" + sep[1]; //$NON-NLS-1$
				}
				if (i == 0) {
					b.append(name);
				} else {
					b.append("\n" + name); //$NON-NLS-1$
				}
			}
		}
		return b.toString();
	}
	
	private void updateXML() {
		try {
			EditXMLFile f = new EditXMLFile();
			HTMLCreator create = new HTMLCreator();
			CreateCoverImage images = new CreateCoverImage();
			ConfigParser cnf = new ConfigParser();
			FileCode code = new FileCode();
			
			TableItem[] s = editorTable.getItems();
		
			String pathBaseName = dirInputXMLName + File.separator + fileInputXMLName;
			String posterExt = f.postersFormat(f.loadData(pathBaseName  + ".xml")); //$NON-NLS-1$
			String thumbnailExt = f.thumbnailFormat(f.loadData(pathBaseName  + ".xml")); //$NON-NLS-1$
			
			f.setFanartURL(urlText.getText());
			f.setGenres(genresStyledText.getText().toString());
			f.setActors(actorsStyledText.getText().toString());
			f.setTitle(titleText.getText());
			f.setOriginalTitle(originalTitleText.getText());
			f.setYear(yearText.getText());
			f.setReleaseDate(releaseText.getText());
			f.setRating(spinner.toString());
			f.setPosterURL(PosterURL.toString());
			f.setFanartURL(FanartURL.toString());
			f.setPlot(plotStyledText.getText());
			f.setOutline(outlineText.getText());
			f.setDirector(directorText.getText());
			f.setCountry(countryText.getText());
			f.setRuntime(runtimeText.getText());
			f.setLanguage(languageText.getText());
			f.setSubtitles(subtitles.toString());
			f.setContainer(containerText.getText());
			f.setVideoCodec(videoCodecText.getText());
			f.setAudioCodec(audioCodecText.getText());
			f.setResolution(resolutionText.getText());
			f.setVideoOutput(videoOutputText.getText());
			f.setVideoSource(videoSourceText.getText());
			f.setFps(fpsText.getText());
			f.setBox(boxStyledText.getText().toString());
			
			File fanart = new File(pathBaseName + ".fanart.jpg"); //$NON-NLS-1$
		
			if (fanart.exists()) {
				String name = fileInputXMLName + ".fanart.jpg"; //$NON-NLS-1$
				f.setFanartFile(code.encode(name));
			} else {
				f.setFanartFile("UNKNOWN"); //$NON-NLS-1$
			}

			String thumbName = fileInputXMLName + "_small." + thumbnailExt; //$NON-NLS-1$
			f.setThumbnail(code.encode(thumbName));

			String posterName = fileInputXMLName + "_large." + posterExt; //$NON-NLS-1$
			f.setPosterFile(code.encode(posterName));
			f.setDetailPoster(code.encode(posterName));
		
			for (int i = 0; i < s.length; i++) {
				String val = s[i].getText(0) + "<BREAK>" + s[i].getText(1) + "<BREAK>" + s[i].getText(2) + "<BREAK>" + s[i].getText(3) + "<BREAK>" + s[i].getText(4); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				rowFiles.add(val);
			}
			f.setRowFiles(rowFiles);
		
			f.saveXMLFile(pathBaseName  + ".xml"); //$NON-NLS-1$
		
			rowFiles.clear();
		
			create.movieHTML(dirInputXMLName + File.separator + fileInputXMLName);
		
			if (Boolean.valueOf(cnf.readConf("setCover"))) { //$NON-NLS-1$
				images.createCoverImage(dirInputXMLName, fileInputXMLName);
			} else {
				images.generate(dirInputXMLName, fileInputXMLName);
			}
		
			shell.close();
			
		} catch(XmlException e) {
			System.out.println(e);
		} catch(IOException e) {
			System.out.println(e);
		}
	}
	
}
